package org.example.everyday;

public class Lc592 {
    public static void main(String[] args) {
        Lc592 lc592 = new Lc592();

        String expression = "-1/2+1/2+1/3";
//        String res = lc592.fractionAddition(expression);
//        System.out.println(res);
        lc592.gcd(4,12);

    }

    public String fractionAddition(String expression) {
        //定义分子和分母
        long denominator = 0, numberator = 1;

        int index = 0, n = expression.length();
        while (index < n){
            //读取分子
            long denominator1  = 0, sign = 1;
            if(expression.charAt(index) == '-' || expression.charAt(index) == '+'){
                sign = expression.charAt(index) == '-' ? -1:1;
                index++;
            }
            while (index < n && Character.isDigit(expression.charAt(index))){
                denominator1 = denominator1 * 10 + expression.charAt(index) - '0';
                index++;
            }
            denominator1 = sign * denominator1;
            index++;
            //读取分母
            long numerator1 = 0;
            while (index < n && Character.isDigit(expression.charAt(index))){
                numerator1 = numerator1 * 10 + expression.charAt(index)-'0';
                index++;
            }
            //分子
            denominator = denominator * numerator1 + denominator1 * numberator;
            //分母
            numberator *= numerator1;

        }
        if(denominator == 0){
            return "0/1";
        }
        long g = gcd(denominator,numberator);
        return Long.toString(denominator / g) + "/" + Long.toString(numberator / g);

    }

    public long gcd(long a, long b){
        long remainder = a % b;
        while (remainder != 0){
            a = b;
            b = remainder;
            remainder = a % b;
        }
        return b;
    }


}
